package ema.tpjee.banking.session;

import java.io.Serializable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import ema.tpjee.banking.dao.BankDao;
import ema.tpjee.banking.dao.CityDao;
import ema.tpjee.banking.dao.CustomerDao;
import ema.tpjee.utils.AppLocator;



public class BankingSession implements Serializable{
	

	private static final long serialVersionUID = -5012050375526860760L;
	
	static int instanceCount=0;
	
	
	private CustomerDao customerDao;
	private BankDao bankDao;
	private CityDao cityDao;
	
	
	public CustomerDao getCustomerDao() {
		return customerDao;
	}

	public BankDao getBankDao() {
		return bankDao;
	}

	public CityDao getCityDao() {
		return cityDao;
	}

	public void createLocalDao() {
		bankDao = AppLocator.getInstance().getComponent("ema.tpjee.banking.dao.local.BankDaoImpl");
		customerDao = AppLocator.getInstance().getComponent("ema.tpjee.banking.dao.local.CustomerDaoImpl");
		cityDao = AppLocator.getInstance().getComponent("ema.tpjee.banking.dao.local.CityDaoImpl");
	}
	
	public void createEjbDao() {
		System.out.println("creating  EjbDao");
		System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
				"org.jnp.interfaces.NamingContextFactory");
		System.setProperty(Context.PROVIDER_URL, "localhost:1099");
		System.setProperty(Context.URL_PKG_PREFIXES,
				"org.jboss.naming:org.jnp.interfaces");
		try {
			bankDao = (BankDao) new InitialContext().lookup("BankDaoImpl/remote");
			customerDao = (CustomerDao) new InitialContext().lookup("CustomerDaoImpl/remote");
			cityDao = (CityDao) new InitialContext().lookup("CityDaoImpl/remote");
	
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}

	public BankingSession() {
		super();
		System.out.println ("creating session "+instanceCount+1);
		createEjbDao();
		if (instanceCount==0){
		  bankDao.populate();
		  customerDao.populate(bankDao.getList().get(0));	
		  System.out.println ("populated for test");
		}
		instanceCount++;
	}	


}
